package com.intellagric.agriecom.controller.agriecom_index;


import com.intellagric.agriecom.module.agriecom_produce.ProduceService;
import com.intellagric.agriecom.module.agriecom_produce_category.AgriecomProduceCategoryService;
import com.intellagric.common.jedis.JedisClient;
import com.intellagric.common.pojo.LayuiDataGridResult;
import com.intellagric.common.utils.CookieUtils;
import com.intellagric.common.utils.JsonUtils;
import com.intellagric.pojo.AgriecomProduce;
import com.intellagric.pojo.AgriecomProduceCategory;
import com.intellagric.pojo.CmsCategoryContent;
import com.intellagric.pojo.SysUser;
import com.intellagric.service.module.cms_content.ContentCategoryService;
import com.intellagric.service.module.cms_content.ContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class IndexController {
    @Autowired
    private AgriecomProduceCategoryService CategoryService;
    @Autowired
    private ContentService contentService;
    @Autowired
    private ProduceService produceService;
    @Autowired
    private ContentCategoryService contentCategoryService;
    @Autowired
    private JedisClient jedisClient;



    @RequestMapping("/index/getMenu")
    @ResponseBody
    public LayuiDataGridResult getMenu(){
        return CategoryService.getMenu();
    }

    @RequestMapping("/index/getContentsBycategoryId")
    @ResponseBody
    public List<CmsCategoryContent> getContentsBycategoryId(String categoryId){

        return contentService.queryContentsByCategoryId(categoryId);
    }

    @RequestMapping("/index/getProudceShow")
    @ResponseBody
    public LayuiDataGridResult getProudceShow(int page, int rows,String order){
        return produceService.getList(page,rows,order);
    }


    @RequestMapping("/index/getNodes")
    @ResponseBody
    public LayuiDataGridResult getNodes(String ParentId){
        return contentCategoryService.queryContentCategoryByPid(ParentId);
    }

    @RequestMapping("/index/getHotProducts")
    @ResponseBody
    public List<AgriecomProduce> getHotProducts(int page, int limit ){
        return produceService.getHotList(page,limit);
    }

    @RequestMapping("/index/getAncestorNode")
    @ResponseBody
    public AgriecomProduceCategory getAncestorNode(String categoryId,int level ){
        return CategoryService.getAncestorNode(categoryId,level);
    }
    @RequestMapping("/index/getUser")
    @ResponseBody
    public SysUser getUser(HttpServletRequest request){
        String token= CookieUtils.getCookieValue( request,"token");
        if(jedisClient.get("SESSION:"+token)==null){
            return null;
        }
        return JsonUtils.jsonToPojo(jedisClient.get("SESSION:"+token), SysUser.class);
    }

}
